// 实现这个项目的构建任务
const {src,dest,parallel,series,watch} = require('gulp')
const del =require('del')
const loadPlugin = require('gulp-load-plugins')
const plugins = loadPlugin()
const browserSync = require('browser-sync')
//创建开发服务器
const bs = browserSync.create()

// const imagemin = require('gulp-imagemin')
const data = {
    menus: [
      {
        name: 'Home',
        icon: 'aperture',
        link: 'index.html'
      },
      {
        name: 'Features',
        link: 'features.html'
      },
      {
        name: 'About',
        link: 'about.html'
      },
      {
        name: 'Contact',
        link: '#',
        children: [
          {
            name: 'Twitter',
            link: 'https://twitter.com/w_zce'
          },
          {
            name: 'About',
            link: 'https://weibo.com/zceme'
          },
          {
            name: 'divider'
          },
          {
            name: 'About',
            link: 'https://github.com/zce'
          }
        ]
      }
    ],
    pkg: require('./package.json'),
    date: new Date()
  }

const clean =()=>{
    return del(['dist','temp'])
}

const style = ()=>{
    return src('src/assets/styles/*.scss',{base:'src'})
        .pipe(plugins.sass())
        .pipe(dest('temp'))
        .pipe(bs.reload({stream:true}))
}

const script = ()=>{
    return src('src/assets/scripts/*.js',{base:'src'})
        .pipe(plugins.babel({presets:['@babel/preset-env']}))
        .pipe(dest('temp'))
        .pipe(bs.reload({stream:true}))
}

const page = ()=>{
    return src('src/**/*.html')
        .pipe(plugins.swig({data, defaults: { cache: false }}))//防止页面缓存没变化
        .pipe(dest('temp'))
        .pipe(bs.reload({stream:true}))
}

// const image = ()=>{
//     return src('src/images/**',{base:'src'})
//         .pipe(imagemin())
//         .pipe(dest('dist'))
// }

// const font = ()=>{
//     return src('src/fonts/**',{base:'src'})
//         .pipe(imagemin())
//         .pipe(dest('dist'))
// }

const extra = () => {
    return src('public/**',)
        .pipe(dest('dist'))
}

const useref = ()=>{
    return src('temp/**/*.html',{base:'temp'})
        .pipe(plugins.useref({searchPath:['temp','.']}))//查找引用文件的路径
        //html  js css
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
            collapseWhitespace: true,
            minifyCSS: true,
            minifyJS: true
        })))
        .pipe(dest('dist'))
}

const serve = ()=>{
    watch('src/assets/styles/*.scss',style)
    watch('src/assets/scripts/*.js',script)
    watch('src/**/*.html',page)

    watch([
        'src/assets/images/**',
        'src/assets/fonts/**',
        'public/**'
    ],bs.reload)
    bs.init({
        notify:false,
        // port:8080,
        // open:false,
        //files:'dist/**', 监听dist下面所有文件修改时热更,当加了pipe(bs.reload({stream:true}))后则不需要监听
        server:{
            baseDir:['temp','src','public'],//按顺序目录依次找
            //会先于baseDir执行
            routes:{
                '/node_modules':'node_modules'
            }
        }
    })
}
const compile = parallel(style,script,page)
//上线前执行的指令
const build =  series(
    clean,
    parallel(
      series(compile, useref),
    //image,
    //font,
      extra
    )
  )
//开发时使用的指令
const develop = series(compile,serve)

module.exports = {
    clean,
    develop,
    build,
}